<?php
/*
 * RepSurveyCount.php
 * Count up the surveys performed in each month for the selected year
 * 
 * Copyright 2013 Eugene Mah <eugenemah@gmail.com>
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 */

require('functions.inc');
require('phplot/phplot.php');  // Use PHPlot to create the graphs http://phplot.sourceforge.net/

/*
 * Establish a connection to the database using PDO. Most of this is
 * taken from PHP in Action by Reiersol, Baker, Shiflett (Manning Publications)
 * and the PHP PDO documentation (http://php.net/manual/en/book.pdo.php)
 */
try {
  $conn = new PDO(
    'mysql:dbname=ComplianceReports;host=localhost',
    'eugenem','imabug');
}
catch (Exception $e) {
  throw new Exception($e->getMessage());
}
// Set PDO to throw more exceptions when encountered
$conn->setAttribute(PDO::ATTR_ERRMODE,
                    PDO::ERRMODE_EXCEPTION);

if (!empty($_POST) || !empty($_GET)) {
  // Figure out where to get the year from
  switch($_SERVER['REQUEST_METHOD']) {
    case 'GET':
      if (!empty($_GET['year'])) $yr = $_GET['year'];
      break;
    case 'POST':
      if (!empty($_POST['year'])) $yr = $_POST['year'];
      break;
  }
  // $_POST has been set. Query the database for the number of surveys by month for that year
  $survCount_query = $conn->prepare("SELECT MONTH(TestDate), COUNT(*) FROM TestDates WHERE YEAR(TestDate)=:YR GROUP BY MONTH(TestDate)");
  $survCount_query->bindParam(':YR', $yr);
  $survCount_query->execute();
  
  // Create an array out of the returned data
  while ($row = $survCount_query->fetch()) {
    $survData[] = array($row[0], $row[1]);
  }
  
  // Create a PHPlot object
  $survPlot = new PHPlot(800,600);
  $survPlot->SetImageBorderType('plain');
  $survPlot->SetDataValues($survData); // Assign the data
  $survPlot->SetPlotType('bars');

  $survPlot->SetPlotAreaWorld(NULL, 0, NULL, NULL);
  $survPlot->SetYTickIncrement(10);

  // Set some graph titles
  $survPlotTitle = "Monthly survey count for " . $yr;
  $survPlot->SetTitle($survPlotTitle);
  $survPlot->SetXTitle('Month');
  $survPlot->SetYTitle('Number of surveys');
  
  // Turn off X-axis ticks and labels
  $survPlot->SetXTickLabelPos('none');
  $survPlot->SetXTickPos('none');
  
  $survPlot->DrawGraph();
}
else {
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
  <title>Radiological Equipment Database</title>
  <meta http-equiv="content-type" content="text/html;charset=utf-8" />
  <meta name="generator" content="Geany 1.22" />
</head>

<body>
<form action="RepSurveyCount.php" method="POST">
<p>Select year:
<?PHP
$year_query = "SELECT DISTINCT YEAR(TestDate) AS Yr FROM TestDates";
$yr_sel = "<select name=\"year\" size=1>\n";
$yr_sel .= "<option value=\"0\">Select year</option>\n";
foreach ($conn->query($year_query) as $row) {
  $yr_sel .= "<option value=\"" . $row['Yr'] . "\">" . $row['Yr'] . "</option>\n";
}
$yr_sel .= "</select>";
echo $yr_sel;
?>
</p>
<p><input type="SUBMIT" value="Search"> / <input type="RESET" value="Reset"> / <a href="index.html" target="_top" >Main</a></p>
</form>

</body>

</html>
<?php
}
?>
